<script setup>
import { reactive, ref, watch, onMounted } from "vue";
import { useRouter } from "vue-router"
import { Delete, Edit, Search, Plus, Refresh } from '@element-plus/icons-vue'

let router = useRouter();
const rules = reactive({})

let customer = ref({})

let columns = [
    { id: "materid", label: "物料编码" },
    { id: "name", label: "物料名称" },
    { id: "photo", label: "物料图片" },
    { id: "SpecificationsAndModels", label: "规格型号" },
    { id: "unit", label: "单位" },
    { id: "MaterialOrProduct", label: "物料/产品" },
    { id: "Category", label: "所属分类" }
]

let isdisabled = ref(true)
let isdisabledreove = ref(true)


let customerId = ref([])

let page = ref({
    current: 1,
    size: 2,
    total: 0,
    records: [
        { "id": 1, "materid": 'wlbm123456', "Category": "aaa", "name": "12312", "photo": "", "SpecificationsAndModels": "", "unit": "kg", "MaterialOrProduct": "物料", "Category": "原材料" },
        { "id": 2, "materid": 'wlbm123457', "Category": "aaa", "name": "12312", "photo": "", "SpecificationsAndModels": "", "unit": "kg", "MaterialOrProduct": "物料", "Category": "原材料" },
        { "id": 3, "materid": 'wlbm123458', "Category": "aaa", "name": "12312", "photo": "", "SpecificationsAndModels": "", "unit": "kg", "MaterialOrProduct": "物料", "Category": "原材料" },
        { "id": 4, "materid": 'wlbm123459', "Category": "aaa", "name": "12312", "photo": "", "SpecificationsAndModels": "", "unit": "kg", "MaterialOrProduct": "物料", "Category": "原材料" }
    ]
})



let shipment_history_id = ref([])

let fuxuankuang = (a) => {
    shipment_history_id.value = a.map(b => b.id)
    if (shipment_history_id.value.length == 1) {
        isdisabled.value = false;
        isdisabledreove.value = false;
    }
    if (shipment_history_id.value.length > 1) {
        isdisabled.value = true;
        isdisabledreove.value = false;
    }
    if (shipment_history_id.value.length == 0) {
        isdisabled.value = true;
        isdisabledreove.value = true;
    }
}

const shuju = ref([
    {
        id: 1,
        label: '物料产品分类',
        children: [
            {
                id: 2,
                label: '产品',
                children: [
                    {
                        id: 4,
                        label: '完成品',
                        children: [
                            { id: 8, label: '电脑' },
                            { id: 9, label: '鼠标' },
                            { id: 10, label: '键盘' }
                        ]
                    },
                    {
                        id: 5,
                        label: '半成品',
                        children: [
                            { id: 11, label: '方便面' },
                            { id: 12, label: '火锅底料' },
                            { id: 13, label: '预制菜' }
                        ]
                    },
                ],
            },
            {
                id: 3,
                label: '物料',
                children: [
                    {
                        id: 6,
                        label: '可食用物料',
                        children: [
                            { id: 14, label: '小鱼干' },
                            { id: 15, label: '调料包' },
                            { id: 16, label: '面饼' }
                        ]
                    },
                    {
                        id: 7,
                        label: '非可食用物料',
                        children: [
                            { id: 17, label: '螺丝' },
                            { id: 18, label: '螺帽' },
                            { id: 19, label: '袋子' }
                        ]
                    },
                ],
            },
        ],
    },
])

const defaultProps = {
    children: 'children',
    label: 'label',
}

const filterText = ref('')
const treeRef = ref()

// 展开所有节点的方法
const expandAllNodes = () => {
  const tree = treeRef.value
  if (tree) {
    const allNodes = tree.store._getAllNodes()
    allNodes.forEach(node => {
      node.expanded = true
    })
  }
}

// 组件挂载时展开所有节点
onMounted(() => {
  expandAllNodes()
})

// 改进的过滤方法 - 展示匹配节点、其父节点和同级节点
const filterNode = (value, data, node) => {
    if (!value) return true
    
    // 如果当前节点匹配
    if (data.label.includes(value)) {
        // 展开所有父节点和同级节点
        expandParentsAndSiblings(node)
        return true
    }
    
    // 检查是否是匹配节点的父节点或同级节点
    return isParentOrSiblingOfMatch(node, value)
}

// 展开父节点和同级节点
const expandParentsAndSiblings = (node) => {
    // 展开所有父节点
    let parent = node.parent
    while (parent) {
        parent.expanded = true
        parent = parent.parent
    }
    
    // 展开同级节点
    if (node.parent) {
        node.parent.childNodes.forEach(child => {
            child.expanded = true
        })
    }
}

// 检查是否是匹配节点的父节点或同级节点
const isParentOrSiblingOfMatch = (node, value) => {
    // 检查是否是父节点
    if (node.childNodes) {
        for (const child of node.childNodes) {
            if (child.data?.label.includes(value)) {
                return true
            }
        }
    }
    
    // 检查是否是同级节点
    if (node.parent) {
        for (const sibling of node.parent.childNodes) {
            if (sibling.data?.label.includes(value)) {
                return true
            }
        }
    }
    
    return false
}

// 监听过滤文本变化
watch(filterText, (val) => {
    treeRef.value.filter(val)
    
    if (val) {
        const tree = treeRef.value
        const allNodes = tree.store._getAllNodes()
        
        // 先收起所有节点
        allNodes.forEach(node => {
            node.expanded = false
        })
        
        // 展开匹配节点及其父节点和同级节点
        allNodes.forEach(node => {
            if (node.data?.label.includes(val)) {
                expandParentsAndSiblings(node)
            }
        })
    } else {
        // 清空搜索时重新展开所有节点
        expandAllNodes()
    }
})

let formRef = ref()

// 重置
let chongzhi = () => {
    formRef.value.resetFields();
    filterText.value = ''
    expandAllNodes() // 重置时重新展开所有节点
}

const formData = ref({
    materialName: '',
})
</script>

<template>
    <div>
        <el-card>
            <template #header>
                <div >
                    <el-row>
                        <el-col :span="6">
                            <el-input 
                                v-model="filterText" 
                                placeholder="输入关键字过滤分类" 
                                size="large" 
                                clearable 
                                style="margin-bottom: 10px;"
                            />
                            
                            <el-tree
                                ref="treeRef"
                                class="filter-tree"
                                :data="shuju"
                                :props="defaultProps"
                                :filter-node-method="filterNode"
                                node-key="id"
                                highlight-current
                                @node-click="(data) => { formData.materialName = data.label }"
                            >
                                <template #default="{ node }">
                                    <span>{{ node.label }}</span>
                                    
                                </template>
                            </el-tree>
                        </el-col>

                        <el-col :span="18">
                            <el-form ref="formRef" inline :model="customer"
                                style="display: flex;justify-content: start;" class="card_wrap">
                                <el-form-item label="物料编码" size="default" prop="customerResourcesName" class="c_item">
                                    <el-input v-model="customer.customerResourcesName" class="sousuo"
                                        placeholder="请输入物料编码" clearable @change=""></el-input>
                                </el-form-item>
                                <el-form-item label="物料名称" size="default" prop="customerResourcesId" class="c_item">
                                    <el-input v-model="customer.customerResourcesId" class="sousuo"
                                        placeholder="请输入物料名称" clearable @change=""></el-input>
                                </el-form-item>
                                <el-form-item class="c_item">
                                    <template #default>
                                        <el-button type="primary" size="default" @click="" icon="Search">搜索</el-button>
                                        <el-button type="info" size="default" :plain=true @click="chongzhi" icon="Refresh">重置</el-button>
                                        <p></p>
                                    </template>
                                </el-form-item>
                            </el-form>


                            <el-table :data="page.records" border stripe @selection-change="fuxuankuang">
                                <el-table-column type="selection" width="55" />
                                <el-table-column v-for="col in columns" :prop="col.id" :key="col.id" :label="col.label">
                                </el-table-column>
                            
                            </el-table>
                        </el-col>
                    </el-row>
                    
                </div>
            </template>

        </el-card>

    </div>
</template>

<style scoped>
.sousuo {
    width: 200px;
}

.card_wrap {
    width: 98%;
    display: flex;
    flex-wrap: wrap;
    justify-content: flex-start;
}

.c_item {
    flex: 1;
    margin: 0px 5px 15px 0;
    width: calc((100%-10px)/3);
    min-width: calc((100% - 10px)/3);
    max-width: calc((100% - 10px)/3);

    &:nth-child(3n) {
        margin-right: 0;
    }
}

.filter-tree :deep(.el-tree-node__content) {
    height: auto;
    padding: 5px 0;
}

</style>